//
//  ScenicoListCtrl.m
//  Traveling
//
//  Created by 尹桥印 on 15/6/4.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "ScenicoListCtrl.h"
#import "HomeDataTool.h"
#import "TravelineItemCell.h"
#import "HomeItemDetailHeaderModel.h"
#import "ModelDetailCtrl.h"
@interface ScenicoListCtrl ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *itemArray;
@property (nonatomic, copy) NSString *keyWord;
@end

@implementation ScenicoListCtrl

-(instancetype)initWithKeyWord:(NSString *)keyWord
{
    self = [super init];
    if (self) {
        self.keyWord = keyWord;
    }
    return self;
}

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"景点景区";
    
    if (ISBLANK(self.keyWord)) {
        self.keyWord = @"";
    }
    
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView headerBeginRefreshing];
    self.tableView.rowHeight = 107;
    [self.tableView setTableFooterView:[UIView new]];
}

-(void)headerRefresh{
    [self requestDataWithMaxId:0];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if (self.itemArray.count) {
        ScenicoItemInfo *item = [self.itemArray lastObject];
        maxId = item.id-1;
    }
    [self requestDataWithMaxId:maxId];
}

-(void)requestDataWithMaxId:(NSInteger)maxId{
    [HomeDataTool getScenicoWithMaxId:maxId searchKey:self.keyWord success:^(ScenicoRes *result) {
        [Utils refreshComplete:self.tableView];
        if (result.isSucc) {
            if (maxId==0) {
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTipsWithView:nil String:@"加载失败"];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TravelineItemCell *cell = [TravelineItemCell initWithTableView:tableView];
    [cell setScenicoItemInfo:self.itemArray[indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ScenicoItemInfo *info = self.itemArray[indexPath.row];
    HomeItemDetailHeaderModel *item = [[HomeItemDetailHeaderModel alloc]init];
    item.contentId = info.id;
    [item setDesc:info.des];
    [item setIconUrl:info.title_img];
    [item setContentUrl:info.content_url];
    [item setCurrentPrice:info.rate_price];
    [item setPrePrice:info.price];
    [item setType:ScenicoDetailType];
    ModelDetailCtrl *ctrl =   [[ModelDetailCtrl alloc]initWithData:item title:@"景点景区详情"];
    [self.navigationController pushViewController:ctrl animated:YES];
}

@end
